﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Data.SqlClient;
namespace _201206QuanLyBanHang.BaoCao
{
    public partial class FormbcCongNo_KhachHang : Form
    {
        BLL_BaoCao bc = new BLL_BaoCao();
        AutoCompleteStringCollection auto2 = new AutoCompleteStringCollection();
        DataSet ds = new DataSet();
        SQLCommand tksql = new SQLCommand();
        public string tencuahang = "";
        string sql1 = "select tencuahang from khachhang";
        string sql2 = "select tenkhuvuc as tencuahang from khuvuc";

        public FormbcCongNo_KhachHang()
        {
            InitializeComponent();
        }

        private void FormbcCongNo_KhachHang_Load(object sender, EventArgs e)
        {
            LoadDataToCollection(sql1);
            //loaddulieuvaoreport(0);
        }

        //Load dữ liệu vào report
        //private void loaddulieuvaoreport(int masp)
        //{
        //    DataTable table = new DataTable();
        //    table.Clear();
        //    //table = Laydulieusanpham(masp);
        //    table = bc.getDSCongNokh(masp);
        //    //reset lai khung hiển thị report
        //    rptViewer.Reset();
        //    //gán tên report cần hiển thị trong khung nhìn viewer
        //    rptViewer.LocalReport.ReportPath = @"D:\Project\Soft\code 03-10-2012 - tháng 10\201206QuanLyBanHang\BaoCao\CongNo_KhachHang.rdlc";// @"\report\rpt_sanpham.rdlc";
        //    // rptViewer.LocalReport.ReportEmbeddedResource = "201206QuanLyBanHang.BaoCao.CongNo_KhachHang.rdlc";
        //    //làm sạch khung nhìn
        //    rptViewer.LocalReport.DataSources.Clear();
        //    ReportDataSource newDataSource = new ReportDataSource("DataSet_CongNotheoKH", table);//("ds_sanpham", table);
        //    rptViewer.LocalReport.DataSources.Add(newDataSource);
        //    //lam tuoi report
        //    // rptViewer.LocalReport.DataSources.Add(new ReportDataSource("ds_sanpham",table);
        //    rptViewer.RefreshReport();
        //}
        private void loaddulieuvaoreport(string strbien)
        {
            DataTable table = new DataTable();
            table.Clear();
            //table = Laydulieusanpham(masp);
            if (radioButton_KhachHang.Checked == true)
            {
                table = bc.getDSCongNokh(strbien);
            }
            if (radioButton_KhuVuc.Checked == true)
            {
                table = bc.getDSCongNoKhuVuc(strbien);                
            }
            
            //reset lai khung hiển thị report
            rptViewer.Reset();
            //gán tên report cần hiển thị trong khung nhìn viewer
            rptViewer.LocalReport.ReportPath = @"D:\Project\Soft\code 03-10-2012 - tháng 10\201206QuanLyBanHang\BaoCao\CongNo_KhachHang.rdlc";// @"\report\rpt_sanpham.rdlc";
            //làm sạch khung nhìn
            rptViewer.LocalReport.DataSources.Clear();
            ReportDataSource newDataSource = new ReportDataSource("DataSet_CongNotheoKH", table);//("ds_sanpham", table);
            //ReportDataSource newDataSource = new ReportDataSource("DataSetDanhSachKhachHang", table);//("ds_sanpham", table);
            
            
            rptViewer.LocalReport.DataSources.Add(newDataSource);
            //lam tuoi report
            // rptViewer.LocalReport.DataSources.Add(new ReportDataSource("ds_sanpham",table);
            rptViewer.RefreshReport();
        }       

        private void LoadDataToCollection(string sql)
        {
            DataConn da = new DataConn();
            AutoCompleteStringCollection auto2 = new AutoCompleteStringCollection();
            SqlDataReader reader;
            auto2.Clear();
            reader = da.ThucHienReader(sql);
            if (reader != null)
            {
                while (reader.Read())
                {
                    auto2.Add(reader["tencuahang"].ToString());
                }
            }
            txtTimKiem.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtTimKiem.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtTimKiem.AutoCompleteCustomSource = auto2;
        }
     
        private void button_BaoCao_Click(object sender, EventArgs e)
        {
            groupPanel_report.Visible = true;
            this.rptViewer.RefreshReport();
            if (radioButton_KhachHang.Checked == true)
            {
                loaddulieuvaoreport(txtTimKiem.Text);                
            }
            if (radioButton_KhuVuc.Checked == true)
            {
                loaddulieuvaoreport(txtTimKiem.Text); 
            }
        }

        private void radioButton_KhuVuc_CheckedChanged(object sender, EventArgs e)
        {
            LoadDataToCollection(sql2);
            txtTimKiem.Text = "";
            groupPanel_report.Visible = false;
        }

        private void radioButton_KhachHang_CheckedChanged(object sender, EventArgs e)
        {
            LoadDataToCollection(sql1);
            txtTimKiem.Text = "";
            groupPanel_report.Visible = false;
        }

        private void groupPanel_report_Click(object sender, EventArgs e)
        {

        }

        
    }
}
